const request = require('requestretry');
class Utils {
    // http协议相关的请求
    async send (path,method,header,params,body) {
        let default_header = {
            Accept: 'application/json',
            'content-Type': 'application/json',

        }
        params = this.safeJsonParse(params);
        header = this.safeJsonParse(header);
        body = this.safeJsonParse(body);
        let opt = {
            headers:Object.assign(default_header,header),
            json: true,
            timeout:3000,
            params,
            body,
            method,
            rejectUnauthorized: false,
            requestCert: true,
        }
        console.log('http opt = ',opt);
        let res =  await request(path, opt);
        return res.body;
    }

    safeJsonParse(data) {
        try {
            return JSON.parse(data);
        } catch (err) {
            console.log('json解析出错 data = ',data);
            return data;
        }
    }
}

let utils = new Utils();
module.exports = utils;
